//
//  THGossipController.swift
//  TongHuoDemo
//
//  Created by rimi on 2017/5/12.
//  Copyright © 2017年 rimi. All rights reserved.
//

import UIKit

class THGossipController: UITableViewController {
    // MARK: - 属性
    // 懒加载数据源数组
    lazy var dataArray: [THGossipModel]? = {
        var arr = [THGossipModel]()
        for _ in 0..<2 {
            arr.append(THGossipModel())
        }
        return arr
    }()
    // 记录选中cell
    var selectCellIndexP: IndexPath?
    
    // MARK: - UI
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置头部视图
        let gossipHeadV = THGossipHeaderView(frame: CGRect(x: 0, y: 0, width: screenW, height: 64))
        gossipHeadV.titleArray = ["全部","娱乐影视","亲子教育","社会万象","生活购物","热点聚焦"]
        tableView.tableHeaderView = gossipHeadV
        
        // 事件回调
        gossipHeadV.titleBtnClickCallback = {(clickBtn) -> () in
            print(clickBtn.currentTitle ?? "")
        }
        
        // 设置cell高度自适应
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.sectionHeaderHeight = 46
        tableView.sectionFooterHeight = 0.1
    }


    
    
    // MARK: - 表格视图数据源方法
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 7
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // 根据模型设置显示cell的重用标识
        let model = dataArray?[indexPath.row]
        let identifier: String
        if (model?.isSelect)! {
            identifier = "detailTextCell"
        }else {
            identifier = "textCell"
        }
        // 获得显示的cell
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! THGossipCell
        
        
        return cell
    }
    
    // MARK: - 表格视图代理方法
    // 组头部视图设置
    // 头部视图的悬停效果需要在表格视图的Plain模式下才能有  Grouped模式下没有
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headView = Bundle.main.loadNibNamed("THTimeLineHeadView", owner: nil, options: nil)?.last as? THTimeLineHeadView
        headView?.monthTtileL.text = "5月"
        headView?.dayTitleL.text = "\(section+3)"
        headView?.dateTextL.text = "5月\(section+3)日"
        
        return headView
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 获得当前点击cell
        let model = dataArray?[indexPath.row]
        model?.isSelect = !(model?.isSelect)!
        
        // 判断第一次点击cell和点击同一个cell
        if selectCellIndexP == nil || selectCellIndexP?.row == indexPath.row {
            tableView.reloadRows(at: [indexPath], with: .automatic)
        }else {
            let oldModel = dataArray?[selectCellIndexP!.row]
            oldModel?.isSelect = false
            tableView.reloadRows(at: [indexPath,selectCellIndexP!], with: .automatic)
        }
        
        // 记录当前选择的cell
        selectCellIndexP = indexPath
        
    }

}
